﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Genrsis.WorkitemTracking.Commands;

namespace Genrsis.WorkitemTracking.Handlers
{
    public class CommandProcessor : ICommandProcessor
    {
        private readonly ICommandHandlerFactory _commandHandlerFactory;

        public CommandProcessor(ICommandHandlerFactory commandHandlerFactory)
        {
            _commandHandlerFactory = commandHandlerFactory;
        }

        public void Issue<TCommand>(TCommand command)
            where TCommand : ICommand
        {
            using (var transaction = new TransactionScope())
            {
                foreach (var handler in _commandHandlerFactory.CreateHandlers<TCommand>(command))
                {
                    handler.Handle(command);
                }

                transaction.Complete();
            }
        }
    }
}